import json
import random

import os
import requests
import csv
from time import sleep
from absl import app

from weibo import Weibo


def get_userInfoById(userId):
    hd = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
          'Accept-Encoding': 'gzip, deflate, br',
          'Connection': 'keep-alive',
          'Host': 'weibo.com',  # 注意这里 手机端为m.weibo
          'Upgrade-Insecure-Requests': '1',
          'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                        'Chrome/86.0.4240.111 Safari/537.36',
          'Cookie': 'SUB=_2A25Ig3cUDeRhGeNI6VAW9S7KzjmIHXVr4fbcrDV8PUNbmtAGLUTAkW9NRbTMY4tVJgw3FMt27Jn1SfIwsqIVXb17; '
                    'SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WFBckzH77QBJMIF9zHnKII_5JpX5KzhUgL.Fo-ceozNSK5cSK'
                    '-2dJLoIp7LxKML1KBLBKnLxKqL1hnLBoMfSozES0-7So-f; SINAGLOBAL=5771663102651.616.1702561167048; '
                    'ULV=1702561167663:1:1:1:5771663102651.616.1702561167048:; '
                    'SCF=Atl-GJH959wN90W-riRIiBlEjMKixuXaDw6Agb7pCDrlR8UhpF8E8u5epfY4m7zOUdk7foa_lq6jxIgAme7MAPs.; '
                    'WBPSESS=Dt2hbAUaXfkVprjyrAZT_Bnth381Bt5TthejOXYwaO19UaofJXB6BG4m1Svs'
                    '-Vokb9sHc_bSGTG75Mj1I2sVQ1zk_KDfrKLECw6geijYmirx9XR2h23zZuzGjTm'
                    '-lduhR2ykcUFMRGd4EGBcjhTkxLN4S7LkZkn13J4COQ156-j0QXH2fa1FJDCMJGVAUWYoH_tJolVGwAw9NHLw5-jT5A==; '
                    'XSRF-TOKEN=g0slSvy9aJbToMuBW7TVHAmU; _s_tentry=weibo.com; '
                    'Apache=5771663102651.616.1702561167048; ALF=1705940036; SSOLoginState=1703348036',
          'User-Agent': 'Mozilla/5.0(Windows NT 10.0;Win64;x64;rv: 89.0) Gecko / 20100101Firefox / 89.0'
          }
    url = 'https://weibo.com/ajax/profile/info?uid=' + userId
    print(url)
    try:
        resp = requests.get(url, headers=hd)
        r_content = resp.content
        m = str(r_content, encoding='utf-8')
        parsed_data = json.loads(m)
        data = parsed_data['data']
        userInfo = data['user']
        weibo = Weibo()
        weibo.user_id = userId
        weibo.followers_count = userInfo['followers_count_str']
        weibo.friends_count = userInfo['friends_count']
        weibo.verified = userInfo['verified']
        weibo.verified_type = userInfo['verified_type']
        print(u'粉丝数：%s\n' % weibo.followers_count)
        print(u'关注数：%s\n' % weibo.friends_count)
        print(u'是否认证：%s\n' % weibo.verified)
        return True, weibo

    except Exception as e:
        print(f'Error processing user_id {userId}: {e}')
        return False, userId
    # print(Weibo.__str__(weibo))
    # writer = CsvWriter(os.getcwd() + '认证类型爬取.csv')
    # writer.write_user(weibo)


class CSVWriter:
    def __init__(self, file_path, fieldnames):
        self.file_path = file_path
        self.fieldnames = fieldnames
        self.counter = 0
        self.csv_file = open(file_path, 'a', newline='', encoding='utf-8')  # 新建一个表格对象进行存储
        self.writer = csv.DictWriter(self.csv_file, fieldnames=self.fieldnames)
        # # 如果文件不存在或者是空的，写入头部
        # if not os.path.exists(file_path) or os.path.getsize(file_path) == 0:
        #     self.writer.writeheader()

    def write_weibo_list(self, weibo_list):
        # 如果文件为空，写入头部
        if os.path.getsize(self.file_path) == 0:
            self.writer.writeheader()
        # 遍历 Weibo 对象列表，将数据写入CSV文件
        for weibo in weibo_list:
            self.writer.writerow({
                'user_id': weibo.user_id,
                'followers_count': weibo.followers_count,
                'friends_count': weibo.friends_count,
                'verified': weibo.verified,
                'verified_type': weibo.verified_type
            })
        print('写入')
        self.csv_file.flush()
        os.fsync(self.csv_file.fileno())

    def write_every_1000(self, weibo):
        self.counter += 1
        if self.counter % 1000 == 0:
            self.write_weibo_list(weibo)
            return True
        return False

    def close(self):
        self.csv_file.close()


def main(_):
    get_userInfoById('1653076903')
    # 读取CSV文件
    input_file_path = os.getcwd() + '\\userid.csv'
    error_file_path = ''
    output_file_path = os.getcwd() + '\\new_data.csv'  # 新的CSV文件路径
    weibo_list = []  # 封装weibo对象
    error_list = []  # 请求失败对象
    with open(input_file_path, 'r', newline='', encoding='utf-8-sig') as csv_file:
        reader = csv.DictReader(csv_file)
        # 初始化一个空列表来存储 Weibo 对象
        weibo_list = []
        error_weibo = Weibo()
        req_count = 0
        # 遍历每一行，访问网站并获取数据，并使用 Weibo 类来存储数据
        catch_count = 0
        # 创建CSVWriter实例并写入CSV文件
        csv_writer = CSVWriter(output_file_path,
                               fieldnames=['user_id', 'followers_count', 'friends_count', 'verified', 'verified_type'])
        for row in reader:
            catch_count += 1
            if catch_count % 100 == 0:
                sleep(20)
            else:
                sleep_num = random.randint(0, 1)
                sleep(sleep_num)
            req_count += 1
            user_id = row['user_id']
            isOk, weibo = get_userInfoById(user_id)
            if isOk:
                weibo_list.append(weibo)
            else:
                error_weibo.user_id = user_id
                error_list.append(error_weibo)  # 请求失败站住位置
            if catch_count == 20                                                                            :
                catch_count = 0
                csv_writer.write_weibo_list(weibo_list)
                weibo_list = []
    csv_writer.close()


if __name__ == '__main__':
    app.run(main)
